<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller
{
    protected $_templates;
	
    public function __construct()
	{
        parent::__construct();
        $this->load->model('login_model','login');
    }

    public function index()
	{  
        $user_name = $this->input->post('user_login');
        $password = md5($this->input->post('user_pass'));
        
        if($this->session->userdata('admin_id')){
            redirect('admincp');
        }
		 
        $this->form_validation->set_rules('user_login',lang('login_name'),'trim|required|callback_check_login');
        $this->form_validation->set_rules('user_pass',lang('login_password'),'trim|required');
        if($this->form_validation->run())
		{
            if($this->login->get_admin($user_name,$password))
			{
                $this->session->set_flashdata('message',lang('login_success'));
                redirect(base_url().'admincp');
            }else{
                $this->session->set_flashdata('error',lang('login_unsuccess_recheck'));
            }
        }          
        $this->load->view('login/index');
    }

    function check_login($user_login)
	{
        $password= md5($this->input->post('user_pass'));
        if($user_login!=''&&$password!=''){
            if($this->login->check_login($user_login,$password)){
                return true;
            }else{
                $this->form_validation->set_message('check_login',lang('login_fail_info'));
                return false;
            }
        }
		else
		{
            $this->form_validation->set_message('check_login',lang('login_name_pass_not_empty'));
            return false;
        }
    }

    function logout()
	{
		$this->session->sess_destroy();
		redirect(base_url());          
    }       
}
?>